Skip to content

Implement isfinite, isinf, isnan ufuncs - #121

Merged
SwayamInSync merged 3 commits into
numpy:mainfrom
juntyr:is-nan-inf-finite
Jul 27, 2025
Merged

Implement isfinite, isinf, isnan ufuncs#121
SwayamInSync merged 3 commits into
numpy:mainfrom
juntyr:is-nan-inf-finite

Conversation

@juntyr

@juntyr juntyr commented Jul 21, 2025

Copy link
Copy Markdown
Contributor

@juntyr

juntyr commented Jul 21, 2025

Copy link
Copy Markdown
Contributor Author

I get some weird runtime warnings in the isfinite and isinf ufuncs:

tests/test_quaddtype.py: 10 warnings
  /workspaces/numpy-user-dtypes/quaddtype/tests/test_quaddtype.py:113: RuntimeWarning: invalid value encountered in isfinite
    quad_result = op_func(quad_val)

tests/test_quaddtype.py: 10 warnings
  /workspaces/numpy-user-dtypes/quaddtype/tests/test_quaddtype.py:113: RuntimeWarning: invalid value encountered in isinf
    quad_result = op_func(quad_val)

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html

Any idea what I'm doing wrong?

@juntyr juntyr mentioned this pull request Jan 16, 2026
11 tasks
@seberg

seberg commented Jul 21, 2025

Copy link
Copy Markdown
Member

IEEE says comparisons with NaNs trigger FPEs (floating point error; FPEs are a flag set on the CPU when one occurs basically). That means normally also nan == inf.
C99 has special comparison functions for this purpose (not that all compilers used to get it right). Maybe sleef gets these warnings right, maybe not. But the best solution would be to avoid the function triggering it. Maybe sleef has a quiet comparison or just a way to test for this without a comparison.

@juntyr

juntyr commented Jul 22, 2025

Copy link
Copy Markdown
Contributor Author

IEEE says comparisons with NaNs trigger FPEs (floating point error; FPEs are a flag set on the CPU when one occurs basically). That means normally also nan == inf. C99 has special comparison functions for this purpose (not that all compilers used to get it right). Maybe sleef gets these warnings right, maybe not. But the best solution would be to avoid the function triggering it. Maybe sleef has a quiet comparison or just a way to test for this without a comparison.

This definitely got me on the right track but wasn't the issue in the end. In fact, Sleef_cast_from_doubleq1(std::numeric_limits<double>::infinity()) raised the flag (no idea why), so I switched to using constants defined with sleef_q (the recommended way).

@seberg

seberg commented Jul 22, 2025

Copy link
Copy Markdown
Member

Seems plausible that the sleef casts uses the wrong type of comparisons internally and nobody noticed/cares (or the compilers mess it up later).

@juntyr one thing I forgot is, that we should definitely add -ftrapping-math (compare NumPy meson.build for Clang builds. The default is just terrible for NumPy here since it honors FPEs (NumPy does, clang doesn't). If you were using Clang, it may well be that just setting this flag fixes the issue. (We should set it either way.)

@juntyr
juntyr marked this pull request as draft July 23, 2025 15:09
@juntyr
juntyr force-pushed the is-nan-inf-finite branch from d5a15f1 to 6e6a88b Compare July 26, 2025 09:12
@juntyr
juntyr marked this pull request as ready for review July 26, 2025 09:13
@juntyr

juntyr commented Jul 26, 2025

Copy link
Copy Markdown
Contributor Author

@SwayamInSync I've rebased this PR and it should now also be good to go

@SwayamInSync SwayamInSync left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @juntyr this looks good, just a small suggestion here.

Comment thread quaddtype/numpy_quaddtype/src/ops.hpp
@SwayamInSync
SwayamInSync merged commit c433a24 into numpy:main Jul 27, 2025
6 checks passed
@SwayamInSync

Copy link
Copy Markdown
Member

Thanks @juntyr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

SleefQuadPrecision arrays are missing isnan, isinf, isfinite, sign, copysign ufuncs

3 participants